<?php
App::uses('AppModel', 'Model');
/**
 * Colonne Model
 *
 * @property Table $Table
 * @property Clerupt $Clerupt
 * @property Descriptif $Descriptif
 * @property Donnee $Donnee
 */
class Colonne extends AppModel {
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'doublon' => array(
				'rule' => array('doublon'),
				'message' => 'Une colonne de même nom est déjà défnie pour cette table',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'pgtypecol_id' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'topPk' => array(
			'boolean' => array(
				'rule' => array('boolean'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'nbcolonne' => array(
				'rule' => array('nbcolonne'),
				'message' => "Le maximum de colonne dans la Pk a été atteint (15)",
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'table_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'longueur' => array(
			'longueur' => array(
				'rule' => array('longueur'),
				'message' => "La longueur est obligatoire pour ce type de donnée",
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'masque' => array(
				'rule' => array('masque'),
				'message' => "La longueur n'est pas valide pour ce type de donnée",
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

/**
 * Contrôles
 *
 * RLEB
 */
	function nbcolonne($check) {       
		if ($check['topPk']==1){
			if (isset($this->data['Colonne']['id']) ){
				$colonne = $this->find( 'count', array('conditions' => array('Colonne.table_id' => $this->data['Colonne']['table_id'], 'Colonne.topPk' => '1', 'Colonne.id !=' => $this->data['Colonne']['id']), 'recursive' => -1) );    
			}else{
			 	$colonne = $this->find( 'count', array('conditions' => array('Colonne.table_id' => $this->data['Colonne']['table_id'], 'Colonne.topPk' => '1'), 'recursive' => -1) );    
			 } 
			if($colonne >= '15'){
				return false;
			}
		}
		return true;
	}

	function doublon($check) {       
		// le tableau $check est passé en utilisant le nom du champ de formulaire comme clé        
		// nous devons extraire la valeur pour rendre la fonction générique   
		 //$check aura comme valeur : array('code_promo' => 'une valeur')   
		 //$limit aura comme valeur : 25        
		 if (isset($this->data['Colonne']['id']) ){
		 	$colonne = $this->find( 'first', array('conditions' => array('Colonne.name' => $this->data['Colonne']['name'], 'Colonne.id !=' => $this->data['Colonne']['id'], 'Colonne.table_id' => $this->data['Colonne']['table_id']), 'recursive' => -1) ); 
		 }else{
		 	$colonne = $this->find( 'first', array('conditions' => array('Colonne.name' => $this->data['Colonne']['name'], 'Colonne.table_id' => $this->data['Colonne']['table_id']), 'recursive' => -1) ); 
		 }
		 if (isset($colonne['Colonne']) ){
		 	return false;
		 }    
		return  (true);    
	}

	function longueur($check) {       
		// le tableau $check est passé en utilisant le nom du champ de formulaire comme clé        
		// nous devons extraire la valeur pour rendre la fonction générique   
		 //$check aura comme valeur : array('code_promo' => 'une valeur')   
		 //$limit aura comme valeur : 25        
		 $controles = $this->Pgtypecol->find( 'first', array('conditions' => array('Pgtypecol.id' => $this->data['Colonne']['pgtypecol_id']), 'recursive' => -1) );     
		 if ($controles['Pgtypecol']['longueurOblig']==1 && !trim($check['longueur'])){
		 	return false;
		 }    
		return  (true);    
	}

	function masque($check) {       
		// le tableau $check est passé en utilisant le nom du champ de formulaire comme clé        
		// nous devons extraire la valeur pour rendre la fonction générique   
		//$check aura comme valeur : array('code_promo' => 'une valeur')   
		//$limit aura comme valeur : 25        
		$controles = $this->Pgtypecol->find( 'first', array('conditions' => array('Pgtypecol.id' => $this->data['Colonne']['pgtypecol_id']), 'recursive' => -1) );   

		if (trim($check['longueur'])){
			if ($controles['Pgtypecol']['masque']=='N' && !ereg('^[1-9]+[0-9]*$',$check['longueur'])){
			 	return false;
			}
			if ($controles['Pgtypecol']['masque']=='N,N' && !ereg('^[1-9]+,{1}[0-9]+$',$check['longueur'])){
			 	return false;
			}
		}
		return true;
	}


	

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Table' => array(
			'className' => 'Table',
			'foreignKey' => 'table_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Pgtypecol' => array(
			'className' => 'Pgtypecol',
			'foreignKey' => 'pgtypecol_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Asscolrupt' => array(
			'className' => 'Asscolrupt',
			'foreignKey' => 'colonne_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Parametrage' => array(
			'className' => 'Parametrage',
			'foreignKey' => 'colonne_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
